import { defineStore } from "pinia";
import {
    reqStudentAll,
    reqStudentList,
    reqStudentAdd,
    reqStudentById,
    reqStudentUpdata,
    reqStudentDelete
} from "@/api/student"
import type {
    studentInfoData,
    studentListFormData,
    studentResponseData,
    studentAllResponseData,
    studentListResponseData
} from "@/api/student/type"
import { StudentState } from "./types/type";
let useStudentStore = defineStore("Student", {
    state: (): StudentState => {
        return {
            students: []
        }
    },
    actions: {
        // 总
        async getStudentAll() {
            let result: studentAllResponseData = await reqStudentAll();
            if (result.status === 0) {
                this.students = (result.data as studentInfoData[])
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 部分
        async getStudentList(data: studentListFormData) {
            let result: studentListResponseData = await reqStudentList(data);
            if (result.status === 0) {
                this.students = (result.data?.data as studentInfoData[])
                return result.data
            } else {
                return Promise.reject(new Error(result.msg));
            }
        },
        // 添加
        async addStudent(data: studentInfoData) {
            let result: studentResponseData = await reqStudentAdd(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // ID获取
        async getStudentById(_id: string) {
            let result: studentResponseData = await reqStudentById(_id)
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))

            }
        },
        // 修改
        async updateStudent(data: studentInfoData) {
            let result: studentResponseData = await reqStudentUpdata(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除
        async deleteStudent(studentId: string) {
            let result: studentResponseData = await reqStudentDelete(studentId);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        getters: {

        }
    }

})
export default useStudentStore